package com.cityfood.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Map;
import java.util.Date;
import java.util.List;

import com.cityfood.common.utils.JsonUtils;
import com.cityfood.pojo.CfProductSpecs;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfOrderMapper;
import com.cityfood.pojo.CfOrder;
import com.cityfood.pojo.CfOrderExample;
import com.cityfood.pojo.CfOrderExample.Criteria;
import com.cityfood.service.OrderService;
import com.cityfood.common.pojo.OrderTradeStatus;

@Service
public class OrderServiceImpl implements OrderService  {
	@Autowired
	private CfOrderMapper mapper;
	
	@Override
	public int createOrder(CfOrder order){
		return mapper.insertSelective(order);
	}
	@Override
	public CfOrder getOrder(int orderId){
		return mapper.selectByPrimaryKey(orderId);
	}
	
	
	@Override
	public List<CfOrder> getMyOrders(int userId){
		CfOrderExample example = new CfOrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		example.setOrderByClause("create_time desc");
		return mapper.selectByExample(example);
	}
	
	@Override
	public int orderPay(int orderId){
		CfOrder order = mapper.selectByPrimaryKey(orderId);
		int tradeStatus = OrderTradeStatus.WAIT_DELIVERY.getStatus();
		order.setTradeStatus((byte) tradeStatus);
		return mapper.updateByPrimaryKey(order);
	}
	
	@Override
	public int orderPersentPay(int orderId){
		CfOrder order = mapper.selectByPrimaryKey(orderId);
		int tradeStatus = OrderTradeStatus.WAIT_PRESENT.getStatus();
		order.setTradeStatus((byte) tradeStatus);
		return mapper.updateByPrimaryKey(order);
	}
	
	@Override
	public int orderDelivery(int orderId){
		CfOrder order = mapper.selectByPrimaryKey(orderId);
		int tradeStatus = OrderTradeStatus.WAIT_COMMENT.getStatus();
		order.setTradeStatus((byte) tradeStatus);
		return mapper.updateByPrimaryKey(order);
	}
	@Override
	public int orderCancel(int orderId){
		CfOrder order = mapper.selectByPrimaryKey(orderId);
		int tradeStatus = OrderTradeStatus.CANCEL.getStatus();
		order.setTradeStatus((byte) tradeStatus);
		return mapper.updateByPrimaryKey(order);
	}
	
	@Override
	public int orderComment(int orderId){
		CfOrder order = mapper.selectByPrimaryKey(orderId);
		int tradeStatus = OrderTradeStatus.FINISH.getStatus();
		order.setTradeStatus((byte) tradeStatus);
		return mapper.updateByPrimaryKey(order);
	}
	
	@Override
	public List<CfOrder> getOrdersByStatus(int userId,int tradeStatus){
		CfOrderExample example = new CfOrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andTradeStatusEqualTo((byte) tradeStatus);
		example.setOrderByClause("create_time desc");
		return mapper.selectByExample(example);
	}
	
	//后台管理-----------------------------------------------------------
	@Override
    public PageInfo<CfOrder> getOrderList(String conditions, int crruentPage) throws ParseException {
        //设置分页的页数和行数
        PageHelper.startPage(crruentPage, 20);
        CfOrderExample example = new CfOrderExample();
        Criteria criteria = example.createCriteria();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

        if(!conditions.equals("{}")){
            @SuppressWarnings("unchecked")
			Map<String,Object> map = JsonUtils.jsonToMap(conditions);
            if(map.containsKey("orderNo")){
                criteria.andOrdernoEqualTo(map.get("orderNo").toString());
            }
            if(map.containsKey("start") && map.containsKey("end")){
                criteria.andCreateTimeBetween(sd.parse(map.get("start").toString()),sd.parse(map.get("end").toString()));
            }
            if(map.containsKey("status")){
                criteria.andTradeStatusEqualTo((Byte)map.get("status"));
            }
            if(map.containsKey("buyAim")){
            	String buyAim = map.get("buyAim").toString();
                criteria.andBuyAimEqualTo((byte)Integer.parseInt(buyAim));
            }
        }

        example.setOrderByClause("create_time desc");
        List<CfOrder> list = mapper.selectByExample(example);

        //5.获取分页信息
        PageInfo<CfOrder> info = new PageInfo<>(list);
        return info;
    }

	@Override
	public int updateOrderStatus(CfOrder order) {
		order.setUpdateTime(new Date());
		return mapper.updateByPrimaryKeySelective(order);
	}
	@Override
	public PageInfo<CfOrder> getMyOrdersAndPage(Integer userId,Integer crruentPage) {
		  //设置分页的页数和行数
        PageHelper.startPage(crruentPage, 10);
        
		CfOrderExample example = new CfOrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		example.setOrderByClause("create_time desc");
		 List<CfOrder> list = mapper.selectByExample(example);
		 
		 //5.获取分页信息
        PageInfo<CfOrder> info = new PageInfo<>(list);
        return info;
	}

	@Override
    public CfOrder getOrder(String orderNo){
        CfOrderExample example = new CfOrderExample();
        Criteria criteria = example.createCriteria();

        criteria.andOrdernoEqualTo(orderNo);

        List<CfOrder> list = mapper.selectByExample(example);

        if(list.size() == 1){
            return list.get(0);
        }else{
            return null;
        }

    }

	
	@Override
	public PageInfo<CfOrder> getMyOrdersByStatusAndPage(Integer userId, Integer status, Integer crruentPage) {
		 //设置分页的页数和行数
        PageHelper.startPage(crruentPage, 10);
        
		CfOrderExample example = new CfOrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andTradeStatusEqualTo(status.byteValue());
		example.setOrderByClause("create_time desc");
		 List<CfOrder> list = mapper.selectByExample(example);
		 
		 //5.获取分页信息
        PageInfo<CfOrder> info = new PageInfo<>(list);
        return info;
	}
	@Override
	public List<CfOrder> getMyOrdersByStatus(Integer userId, Integer status) {
		CfOrderExample example = new CfOrderExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andTradeStatusEqualTo(status.byteValue());
		List<CfOrder> list = mapper.selectByExample(example);
		return list;
	}


}
